<?php
//var_dump($this->data);
if ( count($this->data[0]) > 0)
{
?>
<h1><?php echo $this->rwcards_fillout_cards_please_fill_out; ?></h1>

<form action="<?php echo $this->action; ?>" id="<?php echo $this->formId; ?>" method="post">
<input type="hidden" name="FORM_SUBMIT" value="<?php echo $this->formId; ?>" /><?php echo $this->messages; ?>
<input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}">
<?php
// $sess holds the various messages, addresses etc from the session.
// $this->rwcards[0] is supposed to hold the card info from the database,
// and gets consulted when/if the session is blank.
?>
<div class="rwcardsImage">
	<p style="text-align:center;"><img src="<?php echo ($this->Session->get('picture')) ? $this->Session->get('picture') : $this->Environment->base . $this->data[0]['path']; ?>" hspace="2" vspace="2" alt="" /></p>
</div>
<div class="rwcardsfull">
	<div class="rwcardsLeftForm">

		<div id="rwNameFrom">
			<div><?php echo $this->fields['rwNameFrom']->generateLabel(); ?></div>
  			<?php echo $this->fields['rwNameFrom']->generateWithError(); ?>
		</div>

		<div id="rwEmailFrom">
			<div><?php echo $this->fields['rwEmailFrom']->generateLabel(); ?></div>
  			<?php echo $this->fields['rwEmailFrom']->generateWithError(); ?>
    	</div>

		<div id="rwMessage">
			<div><?php echo $this->fields['rwMessage']->generateLabel(); ?></div>
  			<?php echo $this->fields['rwMessage']->generateWithError(); ?>
		</div>

	</div>
    <div class="rwcardsRightForm">
		<?php
		if ( isset($sess['rwCardsFormEmailTo']) )
		{
			for ($i = 0; $i < count(@$sess['rwCardsFormEmailTo']); $i++)
			{
			?>
			<div class="rwcardsReceiver">
				<div><?php echo $this->fields['rwcardsReceiver']->generateLabel(); ?></div>
  				<?php echo $this->fields['rwcardsReceiver']->generateWithError(); ?>

				<div><?php echo $this->fields['rwCardsFormEmailTo']->generateLabel(); ?></div>
  				<?php echo $this->fields['rwCardsFormEmailTo']->generateWithError(); ?>
			</div>
		<?php
			}
		}
		else
		{
		?>
			<div class="rwcardsReceiver">
				<div><?php echo $this->fields['rwcardsReceiver']->generateLabel(); ?></div>
  				<?php echo $this->fields['rwcardsReceiver']->generateWithError(); ?>

				<div><?php echo $this->fields['rwCardsFormEmailTo']->generateLabel(); ?></div>
  				<?php echo $this->fields['rwCardsFormEmailTo']->generateWithError(); ?>
			</div>
		<?php
		}
		?>

		<div id="moreReceivers"><?php echo $this->rwcards_fillout_cards_add_receiver;?></div>
		<div id="lessReceivers" style="visibility:hidden;"><?php echo $this->rwcards_fillout_cards_remove_receiver;?></div>

		<?php if (isset($this->fields['rwCardsCaptcha'])): ?>
		<div class="widget">
			<div><label for="ctrl_captcha"><?php echo $this->fields['rwCardsCaptcha']->generateQuestion(); ?><span class="mandatory">*</span></label></div>
		  <?php echo $this->fields['rwCardsCaptcha']->generateWithError(); ?>
		</div>
		<?php endif; ?>
		<?php
		}
		?>
    </div>
	<div class="rwcardsClr"></div>
	<div class="rwcards_buttons_bottom">
		<input type="button" id="rwCardsBack" value="<?php echo $this->rwcards_fillout_cards_back;?>" class="button rwcards_button" />
		<input type="submit" name="submit" id="rwCardsPreviewCard" value="<?php echo $this->rwcards_fillout_cards_preview_card; ?>" class="rwcards_button" />
		<input type="submit" name="submit" id="rwCardsSendCard" value="<?php echo $this->rwcards_fillout_cards_send_card;?>" class="rwcards_button" />
		<input type="hidden" name="additionalReceivers" id="additionalReceivers" value="">
		<input type="hidden" name="picture" id="picture" value="<?php echo($this->Session->get('picture')) ? $this->Session->get('picture') : $this->data[0]['path']; ?>">

	</div>
</div>
</form>
<script type="text/javascript">//<![CDATA[
(function($) {
	if ($$('.rwcardsReceiver').length >= 2) {
		$('lessReceivers').setStyle('visibility', 'visible');
	}

	$('moreReceivers').addEvent('click', function() {
		$$('.rwcardsReceiver')[0].clone().injectAfter( $$('.rwcardsReceiver')[$$('.rwcardsReceiver').length - 1 ]);
		$('lessReceivers').setStyle('visibility', 'visible');
		$$('.rwcardsReceiver')[$$('.rwcardsReceiver').length - 1 ].getElements('input').set('value', '');
		var t = $$('.rwcardsReceiver').getChildren('input');
		for(var i = 1; i< t.length; i++){
			var receiver = t[i].getProperty('name')[0];
			var receiverMail = t[i].getProperty('name')[1];

			t[i][0].setProperty('name', 'rwcardsReceiver' + i);
			t[i][1].setProperty('name', 'rwCardsFormEmailTo' + i);

/*
			t[i][0].setProperty('name', 'rwcardsReceiver[]');
			t[i][1].setProperty('name', 'rwCardsFormEmailTo[]');
*/
		};
		$('additionalReceivers').setProperty('value', (t.length - 1));
	});

	$('lessReceivers').addEvent('click', function() {
		if ($$('.rwcardsReceiver').length <= 2) {
			$('lessReceivers').setStyle('visibility', 'hidden');
		}
		$$('.rwcardsReceiver')[$$('.rwcardsReceiver').length - 1 ].dispose();
	});
})(document.id);
//]]></script>

<?php
$this->sessionId = (empty($this->sessionId)) ? '' : $this->sessionId;
?>
<script type="text/javascript">//<![CDATA[
(function($) {
	$('rwCardsBack').addEvent('click', function() {
		document.location.href='<?php echo $this->generateFrontendUrl(array(),  $this->alias . '/view/rwconecategory/category_id/' . $this->category_id . '/reWritetoSender/' . $this->reWritetoSender . '/sessionId/' . $this->sessionId . '/' .  $this->alias);?>';
	});
})(document.id);
//]]></script>
